home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / abbrev4 / ablookup.cb < prev    next >
Text File  |  1989-11-03  |  5KB  |  135 lines

  1. #define TRUE 1
  2.  
  3. /*************************************************************************
  4. *                                                                                                *
  5. *     ABLOOKUP.CB                                                                                 *
  6. *                                                                                                  *
  7. *     This contains the utility files used both by "abbrev" and                 *
  8. *     "suffix".  Either package may be used alone in conjunction                 *
  9. *     with this file, and suffix no longer requires the full                     *
  10. *     abbrev to be used.                                                                     *
  11. *                                                                                                  *
  12. *     Larry DeMar   10-19-89.                                                                 *
  13. *                                                                                                *
  14. **************************************************************************
  15. *                                                                        *
  16. *      AB_LOOKUP                                                                                 *
  17. *                                                                                                  *
  18. *      This is called to look up a string in an abbreviation file.             *
  19. *                                                                                                  *
  20. *      Parameter 0 = file part of file name to look in                             *
  21. *      Parameter 1 = string to lookup                                                     *
  22. *      Parameter 2 = global buffer id of system buffer we're using             *
  23. *      Parameter 3 = filename for system buffer                                         *
  24. *                                                                                                  *
  25. *      If parameter 2 is non-zero, then it is the id of a                         *
  26. *      system buffer containing the lookup file.  Else, we                         *
  27. *      create the system buffer.  Read the lookup file in,                         *
  28. *      and put the new buffer id to parameter 2.                                     *
  29. *                                                                                                  *
  30. *      If the string is found to start in column 1 of the                         *
  31. *      lookup file, then the "return string" is on the                             *
  32. *      rest of the line in the file.    The return string                             *
  33. *      is put back to parameter 1.                                                         *
  34. *                                                                                                  *
  35. *      This returns TRUE of FALSE based on whether the lookup                     *
  36. *      succeeded.                                                                                 *
  37. *                                                                        *
  38. *************************************************************************/
  39. ab_lookup (...)
  40. {
  41.     string file_part,
  42.              abbreviation,
  43.              my_search_string,
  44.              sys_file_name;
  45.  
  46.     int orig_buffer,
  47.          system_id,
  48.          return_code;
  49.  
  50.     get_parm (0, file_part);
  51.     get_parm (1, abbreviation);
  52.     get_parm (2, system_id);
  53.     get_parm (3, sys_file_name);
  54.  
  55.     orig_buffer = inq_buffer ();
  56.     if (                                        // abbrev file in buffer?                     
  57.         system_id)
  58.         set_buffer (system_id);                       // yep...go there                 
  59.     else
  60.         {
  61.         system_id = create_sys_buf (file_part, sys_file_name);         // no...create it. 
  62.         put_parm (2, system_id);                                 // and pass it back. 
  63.         }
  64.     top_of_buffer ();
  65.     sprintf (my_search_string, "<%s[ \t]\\c", abbreviation);
  66.     if (return_code = search_fwd (my_search_string, 1, 0))
  67.         put_parm (1, trim (ltrim (read ())));
  68.     set_buffer (orig_buffer);
  69.     return return_code;
  70. }
  71.  
  72. /*************************************************************************
  73. *                                                                        *
  74. *      CREATE_SYS_BUF                                                                         *
  75. *                                                                                                  *
  76. *      This is called to create the system buffer and read in                     *
  77. *      the abbreviation file.                                                                 *
  78. *                                                                                                  *
  79. *      It returns the id of the buffer it creates.                                     *
  80. *                                                                        *
  81. *************************************************************************/
  82. create_sys_buf (...)
  83. {
  84.     int buf_id;
  85.     string file_part,
  86.          buffer_name;
  87.  
  88.     get_parm (0, file_part);
  89.     get_parm (1, buffer_name);
  90.     buf_id = create_buffer ("abbrev_buf", buffer_name, TRUE);
  91.     set_buffer (buf_id);
  92.     read_file (get_abbrev_file (file_part));         // read in the file          
  93.     return buf_id;
  94. }
  95.  
  96. /*************************************************************************
  97. *                                                                        *
  98. *      GET_ABBREV_FILE                                                                         *
  99. *                                                                                                  *
  100. *      This is called to return the full path of the abbrev                         *
  101. *      directory file.                                                                         *
  102. *                                                                                                  *
  103. *              1) if BABBREV is in environment, then it is                             *
  104. *                  used as the path.                                                         *
  105. *                                                                                                  *
  106. *              2) if BPATH is in environment, then it is                             *
  107. *                  used as the path.                                                         *
  108. *                                                                                                  *
  109. *              3) otherwise "C:\" is used as the path.                                 *
  110. *                                                                        *
  111. *************************************************************************/
  112.  
  113. #define ENV_1 "BABBREV"
  114. #define ENV_2 "BPATH"
  115. #define DEFAULT_PATH "c:"
  116.  
  117. get_abbrev_file (...)
  118. {
  119.     string abbrev_path,
  120.          file_part;
  121.  
  122.     int semi_spot;
  123.  
  124.     get_parm (0, file_part);
  125.     if ((abbrev_path = inq_environment (ENV_1)) == "")
  126.         if ((abbrev_path = inq_environment (ENV_2)) == "")
  127.             abbrev_path = DEFAULT_PATH;
  128.         else
  129.             if (semi_spot = rindex (abbrev_path, ";"))
  130.                 abbrev_path = substr (abbrev_path, semi_spot + 1);
  131.     return trim (ltrim (abbrev_path)) + ("\\" + file_part);
  132. }
  133.  
  134.  
  135.